---@class StoryMgr
StoryMgr = {
    willChap = 0,
    showType = 0,
    cur_story_id = 0,
    cur_chapter_id = 0,
}
local this = StoryMgr
this.storyIndex = nil


function StoryMgr.getStoryNPCTableDataById(id)
    if id == nil then
        return G.dataTable["t_storynpc"];
    end
    return G.dataTable["t_storynpc"][tostring(id)];
end

function StoryMgr.getStoryTableDataByStoryId(storyId)
    if storyId == nil then
        return G.dataTable["t_storymain"];
    end
    return G.dataTable["t_storymain"][tostring(storyId)];
end

function StoryMgr.getStoryTableDataByStageId(stageId, ishard)
    ishard = ishard and 2 or 1;
    this.storyTableData = this.storyTableData or {};
    if this.storyTableData[ishard] == nil then
        this.storyTableData[ishard] = {};
        for k, v in pairs(G.dataTable["t_storymain"]) do
            if v.f_StoryMode == ishard then
                this.storyTableData[ishard][tostring(v.f_mapid)] = v;
            end
        end
    end
    if stageId == nil then
        return this.storyTableData[ishard];
    end
    return this.storyTableData[ishard][tostring(stageId)];
end

function StoryMgr.ShowStoryByStageId(stageId, callback, ishard)
    if GetPlayerPrefs("StoryBother") == "1" then
        return false
    end
    if stageId <= 0 then
        if callback then
            callback()
        end
        return false
    end
    local storyTable = this.getStoryTableDataByStageId(stageId, ishard)
    if storyTable then
        --开始剧情
        return StoryMgr.ShowStoryById(storyTable.f_StoryID,nil, callback)
    end
end
function StoryMgr.ShowStoryById(storyId,type,callback)
    local cfg = this.getStoryTableDataByStoryId(storyId)
    if not cfg then
        logError("找不到t_storymain by id："..storyId)
        return false
    end
    SetPlayerPrefs("StoryRead"..storyId,"1")
    FireEvent(Config.EventType.StoryRead)
    if not type then
        type = EnumConst.StoryShowType.passLevel
    end

    this.showType = type
    this.willChap=storyId;
    this.callback = callback
    showUI(PanelNames.UIStory,storyId);
    closeUI(PanelNames.UIBattleSuccess);
    closeUI(PanelNames.UIBattleFail);
    return true
end
function StoryMgr.RecordStory(willID)
    logicMgr.StoryMgr.willChap=willID;
    showUI(PanelNames.UIStory);
end
function StoryMgr.SetIndex(index)
    this.storyIndex = index
end
---startStage 开始关卡(包含)
---endStage 结束关卡(不包含)
function StoryMgr.HasStoryInRankLevel(startStage,endStage, ishard)
    ishard = ishard and 2 or 1;
    local cfg = dataTable.getDataTable("t_storymain")
    for k,v in pairs(cfg) do
        if v.f_StoryMode == ishard and v.f_mapid >= startStage and v.f_mapid < endStage  then
            return true
        end
    end
    return false
end
return StoryMgr